package com.uphone.kingmall.main;

import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;

import androidx.swiperefreshlayout.widget.SwipeRefreshLayout;

import com.blankj.utilcode.util.ActivityUtils;
import com.blankj.utilcode.util.LogUtils;
import com.china.zhuoyue.R;
import com.google.gson.JsonSyntaxException;
import com.lzy.okgo.model.HttpParams;
import com.lzy.okgo.model.Response;
import com.uphone.kingmall.activity.order.AllOrderActivity;
import com.uphone.kingmall.activity.order.OrderRefundListActivity;
import com.uphone.kingmall.activity.personal.AttentionStoreActivity;
import com.uphone.kingmall.activity.personal.FavoritesActivity;
import com.uphone.kingmall.activity.personal.HistoryListActivity;
import com.uphone.kingmall.activity.personal.LiuYanActivity;
import com.uphone.kingmall.activity.personal.MineHaggleActivity;
import com.uphone.kingmall.activity.personal.MyTeamActivity;
import com.uphone.kingmall.activity.personal.RechargeActivity;
import com.uphone.kingmall.activity.personal.SmallChargeActivity;
import com.uphone.kingmall.activity.personal.TransferAccountsActivity;
import com.uphone.kingmall.activity.personal.VipBuyActivity;
import com.uphone.kingmall.activity.personal.VipCenterActivity;
import com.uphone.kingmall.activity.personal.YongJinListActivity;
import com.uphone.kingmall.activity.personal.set.SettingActivity;
import com.uphone.kingmall.base.BaseFragment;
import com.uphone.kingmall.base.TestActivity;
import com.uphone.kingmall.bean.UserInfosBean;
import com.uphone.kingmall.bean.UserRoleInfoBean;
import com.uphone.kingmall.listener.onNormalRequestListener;
import com.uphone.kingmall.login.LoginActivity;
import com.uphone.kingmall.utils.CommonUtil;
import com.uphone.kingmall.utils.ConstansUtils;
import com.uphone.kingmall.utils.GlideUtils;
import com.uphone.kingmall.utils.GsonUtils;
import com.uphone.kingmall.utils.MyUrl;
import com.uphone.kingmall.utils.OkGoUtils;
import com.uphone.kingmall.utils.SharedPreferencesHelper;
import com.uphone.kingmall.utils.intent.IntentUtils;
import com.uphone.kingmall.utils.web.BaseWebActivity;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;
import butterknife.Unbinder;
//import io.rong.imlib.model.CSCustomServiceInfo;

/**
 * author : SuMeng
 * e-mail : 986335838@qq.com
 * time   : 2019/02/15
 * desc   : 我的fragment
 */
public class MyFragment extends BaseFragment {
    @BindView(R.id.iv_user_head)
    ImageView ivUserHead;
    @BindView(R.id.ll_shoucangjia)
    LinearLayout llShoucangjia;
    @BindView(R.id.ll_guanzhu)
    LinearLayout llGuanzhu;
    @BindView(R.id.ll_zuji)
    LinearLayout llZuji;
    @BindView(R.id.ll_title)
    RelativeLayout llTitle;
    @BindView(R.id.tv_yue_name)
    TextView tvYueName;
    @BindView(R.id.tv_user_name)
    TextView tvUserName;
    @BindView(R.id.tv_user_phone)
    TextView tvUserPhone;
    @BindView(R.id.tv_yue_num)
    TextView tvYueNum;
    @BindView(R.id.tv_yue_chongzhi)
    TextView tvYueChongzhi;
    @BindView(R.id.tv_yue_zhuanzhang)
    TextView tvYueZhuanzhang;
    @BindView(R.id.tv_jifen_name)
    TextView tvJifenName;
    @BindView(R.id.shop_cart_refresh)
    SwipeRefreshLayout shopCartRefresh;
    @BindView(R.id.tv_jifen_num)
    TextView tvJifenNum;
    @BindView(R.id.tv_jifen_chongzhi)
    TextView tvJifenChongzhi;
    @BindView(R.id.tv_jifen_zhuanzhang)
    TextView tvJifenZhuanzhang;
    @BindView(R.id.tv_lingqian_name)
    TextView tvLingqianName;
    @BindView(R.id.tv_lingqian_num)
    TextView tvLingqianNum;
    @BindView(R.id.tv_lingqian_chongzhi)
    TextView tvLingqianChongzhi;
    @BindView(R.id.tv_lingqian_chakan)
    TextView tvLingqianChaKan;
    @BindView(R.id.ll_2)
    LinearLayout ll2;
    @BindView(R.id.tv_chakanquanbu)
    TextView tvChakanquanbu;
    @BindView(R.id.tv_daifukuan)
    TextView tvDaifukuan;
    @BindView(R.id.tv_daifahuo)
    TextView tvDaifahuo;
    @BindView(R.id.tv_daishouhuo)
    TextView tvDaishouhuo;
    @BindView(R.id.tv_pingjia)
    TextView tvPingjia;
    @BindView(R.id.tv_tuikuan)
    TextView tvTuikuan;
    @BindView(R.id.ll_dingdan)
    LinearLayout llDingdan;
    @BindView(R.id.rl_ruzhu)
    RelativeLayout rlRuzhu;
    @BindView(R.id.rl_jiaoyi)
    RelativeLayout rlJiaoyi;
//    @BindView(R.id.rl_tuijian)
//    RelativeLayout rlTuijian;
    @BindView(R.id.rl_liuyan)
    RelativeLayout rlLiuyan;
    @BindView(R.id.rl_shezhi)
    RelativeLayout rlShezhi;
    @BindView(R.id.tv_collect_num)
    TextView tvCollectNum;
    @BindView(R.id.tv_follow_num)
    TextView tvFollowNum;
    @BindView(R.id.tv_foot_num)
    TextView tvFootNum;
    @BindView(R.id.rl_haggle)
    RelativeLayout rlHaggle;
    Unbinder unbinder;
    @BindView(R.id.tv_tag)
    TextView tvTag;
    @BindView(R.id.tv_tag0)
    TextView tvTag0;
    @BindView(R.id.tv_tag1)
    TextView tvTag1;
    @BindView(R.id.tv_tag2)
    TextView tvTag2;
    @BindView(R.id.tv_tag3)
    TextView tvTag3;
    @BindView(R.id.tv_tag4)
    TextView tvTag4;
    @BindView(R.id.tv_vip_normal)
    TextView tvVipNormal;
    @BindView(R.id.img_level)
    ImageView imgLevel;
    @BindView(R.id.linear_top)
    LinearLayout linearTop;
    @BindView(R.id.tv_vip_tips)
    TextView tvVipTips;
    @BindView(R.id.tv_empty_weight)
    View tvEmptyWeight;
    @BindView(R.id.tv_open_vip)
    TextView tvOpenVip;
    @BindView(R.id.linear_vip)
    LinearLayout linearVip;
    @BindView(R.id.rl_vip)
    RelativeLayout RelVip;
    Unbinder unbinder1;
    private UserInfosBean.UserInfoBean user;

    @Override
    public int getLayoutContentId() {
        return R.layout.kingmall_fragment_my;
    }

    @Override
    public void onResume() {
        super.onResume();
        loadData();
    }


    @Override
    public void loadJPush() {
        super.loadJPush();
        LogUtils.e("jpush");
        loadData();
    }

    private void loadData() {
        if (TextUtils.isEmpty(SharedPreferencesHelper.getToken())) {
            tvUserName.setText("用户昵称");
            tvUserPhone.setText("账号:");
            GlideUtils.getInstance().loadCircleImage(getActivity(), R.mipmap.icon, ivUserHead);
            tvYueNum.setText("¥0");
            tvJifenNum.setText("0");
            tvLingqianNum.setText("¥0");
            tvCollectNum.setText("0");
            tvFollowNum.setText("0");
            tvFootNum.setText("0");
            tvVipNormal.setVisibility(View.VISIBLE);
            imgLevel.setVisibility(View.GONE);
            return;
        }
        OkGoUtils.normalRequest(MyUrl.getUserRoleInfo, new HttpParams("token", SharedPreferencesHelper.getToken()), new onNormalRequestListener() {
            @Override
            public void onSuccess(String body, int code) {
                if (code == 0) {
                    UserRoleInfoBean bean = GsonUtils.getGson().fromJson(body, UserRoleInfoBean.class);
                    //普通用户
                    if (bean.userRoleInfo == null || (bean.userRoleInfo.shop == null
                            && bean.userRoleInfo.supplier == null)) {
                        if (rlRuzhu != null) {
                            rlRuzhu.setVisibility(View.GONE);
                        }
                    } else {
                        //供应商或者商家或者多角色
                        if (rlRuzhu != null) {
                            rlRuzhu.setVisibility(View.VISIBLE);
                        }
                    }
                } else {
                    if (rlRuzhu != null) {
                        rlRuzhu.setVisibility(View.GONE);
                    }
                }
            }

            @Override
            public void onError(Response<String> response) {
                if (rlRuzhu != null) {
                    rlRuzhu.setVisibility(View.GONE);
                }
            }
        });
        OkGoUtils.normalRequest(MyUrl.getUserInfo, null, new onNormalRequestListener() {
            @Override
            public void onSuccess(String response, int code) {
                if (code == 0) {
                    try {
                        UserInfosBean bean = GsonUtils.getGson().fromJson(response, UserInfosBean.class);
                        user = bean.getUserInfo();
                        if (user != null) {
                            if (tvUserName == null)
                                return;
                            tvUserName.setText("" + user.getUserName());
                            tvUserPhone.setText("" + user.getUserMobile());
                            GlideUtils.getInstance().loadCircleImage(getActivity(), user.getUserPhoto(), ivUserHead);
                            tvYueNum.setText("¥" + user.getYue());
                            tvJifenNum.setText("" + user.getScore());
                            tvLingqianNum.setText("¥" + user.getLingQian());
                            tvCollectNum.setText("" + user.getCollectGoodsCount());
                            tvFollowNum.setText("" + user.getCollectShopCount());
                            tvFootNum.setText("" + user.getUserFootCount());

                            if (user.getCollectdaiTuiCount() > 0) {
                                tvTag4.setVisibility(View.VISIBLE);
                                tvTag4.setText(user.getCollectdaiTuiCount() + "");
                            } else {
                                tvTag4.setVisibility(View.GONE);
                            }
                            if (user.getCollectdaiPingCount() > 0) {
                                tvTag3.setVisibility(View.VISIBLE);
                                tvTag3.setText(user.getCollectdaiPingCount() + "");
                            } else {
                                tvTag3.setVisibility(View.GONE);
                            }
                            if (user.getCollectdaiShouCount() > 0) {
                                tvTag2.setVisibility(View.VISIBLE);
                                tvTag2.setText(user.getCollectdaiShouCount() + "");
                            } else {
                                tvTag2.setVisibility(View.GONE);
                            }
                            if (user.getCollectdaiFaCount() > 0) {
                                tvTag1.setVisibility(View.VISIBLE);
                                tvTag1.setText(user.getCollectdaiFaCount() + "");
                            } else {
                                tvTag1.setVisibility(View.GONE);
                            }
                            if (user.getCollectdaiFuCount() > 0) {
                                tvTag0.setVisibility(View.VISIBLE);
                                tvTag0.setText(user.getCollectdaiFuCount() + "");
                            } else {
                                tvTag0.setVisibility(View.GONE);
                            }
                            if (user.getCollectmsgCount() > 0) {
                                tvTag.setVisibility(View.VISIBLE);
                                tvTag.setText(user.getCollectmsgCount() + "");
                            } else {
                                tvTag.setVisibility(View.GONE);
                            }

                            if (user.getUserType() == 1) {
                                tvJifenChongzhi.setVisibility(View.GONE);
                                tvJifenZhuanzhang.setVisibility(View.GONE);
                                tvYueChongzhi.setVisibility(View.GONE);
                                tvYueZhuanzhang.setVisibility(View.GONE);

                                tvVipNormal.setVisibility(View.VISIBLE);
                                imgLevel.setVisibility(View.GONE);
                                tvVipTips.setText("开通会员预计为你每年节省8397元");
                                tvOpenVip.setVisibility(View.VISIBLE);
                                tvEmptyWeight.setVisibility(View.VISIBLE);


                                RelVip.setVisibility(View.GONE);

                            } else if (user.getUserType() == 2) {
                                tvJifenChongzhi.setVisibility(View.GONE);
                                tvJifenZhuanzhang.setVisibility(View.GONE);
                                tvYueChongzhi.setVisibility(View.VISIBLE);
                                tvYueZhuanzhang.setVisibility(View.GONE);

                                tvVipNormal.setVisibility(View.GONE);
                                imgLevel.setVisibility(View.VISIBLE);
                                imgLevel.setImageResource(R.mipmap.me_btn_vip);
                                tvVipTips.setText("VIP会员累计为你节省" + user.getTotalSave() + "元");
                                tvOpenVip.setVisibility(View.GONE);
                                tvEmptyWeight.setVisibility(View.GONE);
                                RelVip.setVisibility(View.VISIBLE);
                            } else {
                                if (user.getShopStatus() == 1) {
                                    tvJifenChongzhi.setVisibility(View.VISIBLE);
                                    tvJifenZhuanzhang.setVisibility(View.VISIBLE);
                                    tvYueChongzhi.setVisibility(View.VISIBLE);
                                    tvYueZhuanzhang.setVisibility(View.VISIBLE);

                                    tvVipNormal.setVisibility(View.GONE);
                                    imgLevel.setVisibility(View.VISIBLE);
                                    imgLevel.setImageResource(R.mipmap.me_btn_business);
                                    tvVipTips.setText("商家会员累计为你节省" + user.getTotalSave() + "元");
                                    tvOpenVip.setVisibility(View.GONE);
                                    tvEmptyWeight.setVisibility(View.GONE);
                                    RelVip.setVisibility(View.VISIBLE);
                                } else {
                                    tvJifenChongzhi.setVisibility(View.GONE);
                                    tvJifenZhuanzhang.setVisibility(View.GONE);
                                    tvYueChongzhi.setVisibility(View.VISIBLE);
                                    tvYueZhuanzhang.setVisibility(View.GONE);

                                    tvVipNormal.setVisibility(View.GONE);
                                    imgLevel.setVisibility(View.VISIBLE);
                                    imgLevel.setImageResource(R.mipmap.me_btn_vip);
                                    tvVipTips.setText("VIP会员累计为你节省" + user.getTotalSave() + "元");
                                    tvOpenVip.setVisibility(View.GONE);
                                    tvEmptyWeight.setVisibility(View.GONE);
                                    RelVip.setVisibility(View.VISIBLE);
                                }
                            }

                            SharedPreferencesHelper.saveShopStatus(user.getShopStatus() + "");
                            SharedPreferencesHelper.saveUserName(user.getUserName());
                            SharedPreferencesHelper.saveUserPhone(user.getUserMobile());
                            SharedPreferencesHelper.saveUserId(user.getUserId() + "");
                            SharedPreferencesHelper.saveUserImg(user.getUserPhoto());
                            SharedPreferencesHelper.saveUserInviteCode(user.getInviteCode() + "");
                        }
                    } catch (JsonSyntaxException e) {
                        e.printStackTrace();
                    }
                }
            }

            @Override
            public void onError(Response<String> response) {

            }
        });
    }

    @Override
    public void initData() {

    }

    @Override
    public void initView(View view) {
        shopCartRefresh.setOnRefreshListener(new SwipeRefreshLayout.OnRefreshListener() {
            @Override
            public void onRefresh() {
                new Handler().postDelayed(new Runnable() {
                    @Override
                    public void run() {
                        if (shopCartRefresh != null)
                            shopCartRefresh.setRefreshing(false);
                    }
                }, 2000);
                loadData();
            }
        });
    }


    @OnClick({R.id.iv_xiaoxi, R.id.iv_user_head, R.id.ll_shoucangjia, R.id.rl_haggle, R.id.rl_group, R.id.ll_guanzhu, R.id.ll_zuji, R.id.tv_yue_chongzhi, R.id.tv_yue_zhuanzhang, R.id.tv_jifen_chongzhi, R.id.tv_jifen_zhuanzhang,
            R.id.tv_lingqian_chakan, R.id.tv_chakanquanbu, R.id.tv_daifukuan, R.id.tv_daifahuo, R.id.tv_daishouhuo, R.id.tv_pingjia,
            R.id.tv_tuikuan, R.id.rl_ruzhu, R.id.rl_jiaoyi, R.id.rl_liuyan, R.id.rl_shezhi, R.id.tv_test, R.id.linear_vip, R.id.rl_vip})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.tv_test://测试
                openActivity(TestActivity.class);
                break;
            case R.id.iv_user_head://登录
                if (TextUtils.isEmpty(SharedPreferencesHelper.getToken())) {
                    CommonUtil.startIntent(getActivity(), LoginActivity.class);
                }
                break;
            case R.id.iv_xiaoxi://消息中心
//                if (TextUtils.isEmpty(SharedPreferencesHelper.getToken())) {
//                    CommonUtil.startIntent(getActivity(), LoginActivity.class);
//                    return;
//                }
//
//                IntentUtils.getInstance().with(getActivity(), MessageActivity.class)
//                        .putInt("msgCount", user.getCollectmsgCount())
//                        .start();
                break;
            case R.id.ll_shoucangjia://收藏夹
                openActivity(FavoritesActivity.class);
                break;
            case R.id.ll_guanzhu://关注店铺
                openActivity(AttentionStoreActivity.class);
                break;
//            case R.id.rl_kefu://客服
//                CSCustomServiceInfo.Builder csBuilder = new CSCustomServiceInfo.Builder();
//                final CSCustomServiceInfo csInfo = csBuilder.nickName(SharedPreferencesHelper.getUserName()).build();
//                RongIM.getInstance().startCustomerServiceChat(getActivity(), ConstansUtils.CSCustomServiceId, "", csInfo);
//                break;
            case R.id.rl_group://我的团队
                CommonUtil.startIntent(getActivity(), MyTeamActivity.class);
                break;
            case R.id.ll_zuji://足迹
                CommonUtil.startIntent(getActivity(), HistoryListActivity.class);
                break;
            case R.id.tv_yue_chongzhi://余额充值
                CommonUtil.startIntentType(getActivity(), RechargeActivity.class, RechargeActivity.TYPE_WALLET);
                break;
            case R.id.tv_yue_zhuanzhang://余额转账
                CommonUtil.startIntentType(getActivity(), TransferAccountsActivity.class, 0);
                break;
            case R.id.tv_jifen_chongzhi://金币充值
                // 充值
                CommonUtil.startIntentType(getActivity(), RechargeActivity.class, RechargeActivity.TYPE_INTEGRAL);
                break;
            case R.id.tv_jifen_zhuanzhang://金币转账
                CommonUtil.startIntentType(getActivity(), TransferAccountsActivity.class, 1);
                break;
            case R.id.tv_lingqian_chakan://佣金查看
                openActivity(SmallChargeActivity.class);
                break;
            case R.id.tv_chakanquanbu:
                ActivityUtils.startActivity(AllOrderActivity.class);
                break;
            case R.id.tv_daifukuan:
                CommonUtil.startIntentType(getActivity(), AllOrderActivity.class, 1);
                break;
            case R.id.tv_daifahuo:
                CommonUtil.startIntentType(getActivity(), AllOrderActivity.class, 2);
                break;
            case R.id.tv_daishouhuo:
                CommonUtil.startIntentType(getActivity(), AllOrderActivity.class, 3);
                break;
            case R.id.tv_pingjia:
                CommonUtil.startIntentType(getActivity(), AllOrderActivity.class, 4);
                break;
            case R.id.tv_tuikuan:
                ActivityUtils.startActivity(OrderRefundListActivity.class);
                break;
            case R.id.rl_haggle://我的砍价
                openActivity(MineHaggleActivity.class);
                break;
            case R.id.rl_ruzhu://商家入驻
                Intent intent = new Intent(getActivity(), BaseWebActivity.class);
                intent.putExtra("title", "商家管理");
                intent.putExtra("url", "http://wechat.91wzsc.com/myShopManage?token=" + SharedPreferencesHelper.getToken());
                startActivity(intent);
                //审核通过
//                if (user != null && user.getShopStatus() == 1) {
////                    openActivity(ShangJiaRuZhuSuccessActivity.class);
//                } else {
//                    GetShopStatus();
//                }
                break;
            case R.id.rl_jiaoyi://交易记录
                IntentUtils.getInstance().with(getActivity(), YongJinListActivity.class).putInt(ConstansUtils.TYPE, YongJinListActivity.TYPE_ORDER).start();
                break;
//            case R.id.rl_tuijian://我的推荐码
//                openActivity(InviteCodeActivity.class);
//                break;
            case R.id.rl_liuyan://我要留言
                openActivity(LiuYanActivity.class);
                break;
            case R.id.rl_shezhi://设置
                openActivity(SettingActivity.class);
                break;
            case R.id.linear_vip: //vip中心
            case R.id.rl_vip:
                if (user.getUserType() == 2 || user.getUserType() == 3) {
                    openActivity(VipCenterActivity.class);
                } else {
                    openActivity(VipBuyActivity.class);
                }
                break;
        }
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        // TODO: inflate a fragment view
        View rootView = super.onCreateView(inflater, container, savedInstanceState);
        unbinder1 = ButterKnife.bind(this, rootView);
        return rootView;
    }

    @Override
    public void onDestroyView() {
        super.onDestroyView();
        unbinder1.unbind();
    }

//    private void GetShopStatus() {
//        OkGoUtils.normalRequestGet(MyUrl.GetShopStatus, new onNormalRequestListener() {
//            @Override
//            public void onSuccess(String json, int code) {
//                if (code == 0) {
//                    GetShopStatusBean bean = GsonUtils.getGson().fromJson(json, GetShopStatusBean.class);
//                    if (bean.getCode() == 0) {
//                        if (bean.getData().getShopStatus() == 1) {
////                            openActivity(ShangJiaRuZhuSuccessActivity.class);
//                            Intent intent = new Intent(getActivity(), BaseWebActivity.class);
//                            intent.putExtra("title", "商家管理");
//                            intent.putExtra("url", "http://wechat.91wzsc.com/myShopManage?token=" + SharedPreferencesHelper.getToken());
//                            startActivity(intent);
//                        } else if (bean.getData().getShopStatus() == 4) {
//                            if (user.getUserType() == 2) {
//                                if (user.getWay() == 1) {
//                                    Bundle bundle = new Bundle();
//                                    bundle.putInt("type", 3);
//                                    openActivity(VipUpgradeActivity.class, bundle);
//                                } else {
//                                    openActivity(VipUpgradeActivity.class);
//                                }
//                            } else {
//                                openActivity(ShangJiaRuZhuActivity.class);
//                            }
//                        } else {
//                            openActivity(ShangJiaRuZhuActivity.class);
//                        }
//                    } else if (bean.getCode() == 101 || bean.getCode() == 102) {
//                        if (user.getWay() == 0) {
//                            Bundle bundle = new Bundle();
//                            bundle.putInt("type", 3);
//                            openActivity(VipBuyActivity.class, bundle);
//                        } else if (user.getWay() == 1) {
//                            Bundle bundle = new Bundle();
//                            bundle.putInt("type", 3);
//                            openActivity(VipUpgradeActivity.class, bundle);
//                        } else {
//                            openActivity(VipUpgradeActivity.class);
//                        }
//                    }
//
//                }
//            }
//
//            @Override
//            public void onError(Response<String> response) {
//
//            }
//        });
//    }

}
